# -*- coding: utf-8 -*-
"""
author:码同学 极光
date:2023/12/3
desc: 
sample: 
"""
import json

import redis

# 创建Redis连接
r = redis.Redis(host='192.168.79.170', port=6379, db=0, password='02afbc26a84dd058')

# r.rpush('my_list', 'item1')
# r.rpush('my_list', 'item2')
re = r.rpop('my_list')
list_items = r.lrange('my_list', 0, -1)
print(list_items)
for item in list_items:
    print(item.decode('utf-8'))

json_dict  = {"test":'1212','pas':'1212'}
#字典转字符串
json_str = json.dumps(json_dict)
r.set('testkey', json_str)
# 读取Redis
value = r.get('testkey')
result = json.loads(value.decode('utf-8'))
print(result.get('test'))



r.zadd('my_zset1', {'n1':3,'n2':10,'n3':5})
# 获取有序集合中所有元素及其分数
for item in r.zrange('my_zset1', 0, -1, withscores=True):
    print(f"Item: {item[0].decode('utf-8')}, Score: {item[1]}")

for item in r.zrevrange('my_zset1', 0, -1, withscores=True):
    print(f"Item: {item[0].decode('utf-8')}, Score: {item[1]}")
